package com.webuy.h5.container

import android.content.Context
import android.text.TextUtils
import timber.log.Timber
import android.graphics.Bitmap
import android.graphics.Bitmap.CompressFormat
import com.tencent.smtt.sdk.MimeTypeMap
import com.tencent.smtt.export.external.interfaces.WebResourceResponse


import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.InputStream
import java.util.HashSet

//import com.tencent.smtt.export.external.interfaces.WebResourceResponse
//import com.tencent.smtt.sdk.WebView;
//import com.tencent.smtt.sdk.WebView
/**
 * author: luoXiong
 * e-mail: 382060748@qq.com
 * date: 2021/7/20 10:46
 * version: 1.0
 * desc:
 */
object GlideImgCacheManager {

    //只缓存白名单中的图片资源
    private val CACHE_IMG_TYPE: HashSet<*> = object : HashSet<Any?>() {
        init {
            add("png")
            add("jpg")
            add("jpeg")
            add("bmp")
            add("webp")
        }
    }

    /**
     * 拦截资源
     * @param url
     * @return 请求结果
     */
    fun interceptRequest(context: Context,url: String): WebResourceResponse? {
        val imgEngine = JlH5Container.getImgEngine() ?: return null
        try {
            val extension = MimeTypeMap.getFileExtensionFromUrl(url)
            if (TextUtils.isEmpty(extension) || !CACHE_IMG_TYPE.contains(extension.toLowerCase())) {
                //不在支持的缓存范围内
                return null
            }
            Timber.d(String.format("开始 glide cache img (%s),url:%s", extension, url))
            val startTime = System.currentTimeMillis()
            //String mimeType = MimeTypeMap.getMimeTypeFromUrl(url);
            var inputStream: InputStream? = null
            // val bitmap = Glide.with(context).asBitmap().diskCacheStrategy(DiskCacheStrategy.ALL).load(url).submit().get()
            val bitmap = imgEngine.img2BitmapOnBackgroundThread(context,url)
            inputStream = getBitmapInputStream(bitmap, CompressFormat.JPEG)
            val costTime = System.currentTimeMillis() - startTime
            if (inputStream != null) {
                Timber.d(String.format("glide cache img(%s ms): %s", costTime, url))
                val res = WebResourceResponse("image/jpg", "UTF-8", inputStream)
                return res
            } else {
                Timber.e(String.format("glide cache error.(%s ms): %s", costTime, url))
            }

        } catch (t: Throwable) {
            t.printStackTrace()
        }
        return null
    }

    /**
     * 将bitmap进行压缩转换成InputStream
     *
     * @param bitmap
     * @param compressFormat
     * @return
     */
    private fun getBitmapInputStream(bitmap: Bitmap, compressFormat: CompressFormat): InputStream? {
        try {
            val byteArrayOutputStream = ByteArrayOutputStream()
            bitmap.compress(compressFormat, 80, byteArrayOutputStream)
            val data = byteArrayOutputStream.toByteArray()
            return ByteArrayInputStream(data)
        } catch (t: Throwable) {
            t.printStackTrace()
        }
        return null
    }


}